﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Collections.Specialized;
using UserDefinedForms.Entities;
using UserDefinedForms.Business;

namespace UDFDesign.GUIComponent.CustomControl
{
    public class SupFormControlMappingObjectcs
    {
        private static ControlListItemService listItemService = new ControlListItemService();

        private DesignableControl control;

        [BrowsableAttribute(false)] 
        public DesignableControl Control
        {
            get { return control; }
            
        }

        [CategoryAttribute("General")]
        public virtual string Name { set; get; }

        [CategoryAttribute("General"), ReadOnlyAttribute(true)]
        public virtual ControlTypes Type { set; get; }

        [CategoryAttribute("General")]
        public virtual string ToolTipText { set; get; }

        [CategoryAttribute("General")]
        public virtual int Status { set; get; }

        [CategoryAttribute("General")]
        public virtual string Description { set; get; }

        [CategoryAttribute("General")]
        public virtual Boolean IsCode { set; get; }

        [CategoryAttribute("General")]
        public virtual Boolean IsSummaryField { set; get; }

        [CategoryAttribute("Appearance")]
        public virtual Font Font { set; get; }

        [CategoryAttribute("Appearance")]
        public virtual Point Location { set; get; }


        [CategoryAttribute("Appearance")]
        public virtual Size Size { set; get; }


        [CategoryAttribute("Appearance")]
        public virtual Boolean Border { set; get; }

        [CategoryAttribute("Appearance")]
        public virtual Boolean Visible { set; get; }

        [CategoryAttribute("Value")]
        public virtual Boolean Enable { set; get; }

        [CategoryAttribute("Value")]
        public virtual string DefaultValue { set; get; }

        [CategoryAttribute("Value")]
        public virtual List<string> List { set; get; }

        public SupFormControlMappingObjectcs(FormControl entity)
        {

            this.DefaultValue = entity.DefaultValue;

            this.Description = entity.Description;
            this.Enable = (bool)entity.Enable;

            TypeConverter tc = TypeDescriptor.GetConverter(typeof(Font));

            this.Font =(Font) tc.ConvertFromString(entity.Font);

            this.Size = new Size((int)entity.Width,(int) entity.Height);
            this.Location = new Point((int)entity.Left,(int) entity.Top);

            TList<ControlListItem> listItemEntities = listItemService.GetByControlId(entity.ControlId);
            this.List =new List<string>();
            for (int i = 0; i < listItemEntities.Count; i++)
            {
                this.List[i] = listItemEntities[i].ItemData;
            }

            this.Name = entity.Name;
            this.Status = entity.Status;
            this.ToolTipText = entity.ToolTipText;
            this.Visible = (Boolean)entity.Visible;
        }

        public SupFormControlMappingObjectcs()
        {

            
        }

        public void setControl(DesignableControl control)
        {
            this.control = control;
            if (control.ControlORM.Border == null)
            {
                control.ControlORM.Border = false;
                this.Border = false;
            }
            if (control.ControlORM.Enable == null)
            {
                control.ControlORM.Enable = true;
                this.Enable = true;
            }
            if (control.ControlORM.Visible == null)
            {
                control.ControlORM.Visible = true;
                this.Visible = true;
            }
            updateInfo();
        }

        public void updateInfo()
        {

            this.DefaultValue = control.ControlORM.DefaultValue;
            
            this.Description = control.ControlORM.Description;
            this.Enable =(bool) control.ControlORM.Enable;
            this.Font = control.Font;
            this.Size = control.Size;
            this.Location = control.Location;
            this.List = control.List;
            this.Name = control.ControlORM.Name;
            this.Status = control.ControlORM.Status;
            this.ToolTipText = control.ToolTipText;
            this.Type = control.ControlType;
            this.Visible = (Boolean)control.ControlORM.Visible;
        }

        public void applyChanges()
        {

            control.ControlORM.DefaultValue = this.DefaultValue;

            control.ControlORM.Description=this.Description ;
            control.ControlORM.Enable = this.Enable;
            control.Font=this.Font;
            control.Size=this.Size;
            control.Location= this.Location;
            control.List=this.List;
            control.ControlORM.Name=this.Name ;
            control.ControlORM.Status=this.Status ;
            control.ToolTipText=this.ToolTipText;
           
            this.Visible = (bool)control.ControlORM.Visible;

            updateInfo();
        }


        public FormControl toEntity()
        {
            this.updateInfo();
            FormControl controlORM = control.ControlORM;
            controlORM.Name = this.Name;
            controlORM.ControlTypeId = (int)control.ControlType;
            controlORM.ToolTipText = this.ToolTipText;
            controlORM.Border = this.Border;
            controlORM.Left = this.Location.X;
            controlORM.Top = this.Location.Y;
            controlORM.Width = this.Size.Width;
            controlORM.Height = this.Size.Height;
            TypeConverter tc = TypeDescriptor.GetConverter(typeof(Font));
            controlORM.Font = tc.ConvertToString(this.Font);
            controlORM.DefaultValue = this.DefaultValue;
            controlORM.Visible = this.Visible;
            controlORM.Enable = this.Enable;
            controlORM.IsSummaryField = this.IsSummaryField;
            controlORM.IsCommon = false;
            controlORM.IsCode = this.IsCode;
            controlORM.IsLayout = true;
            controlORM.Status = this.Status;
            controlORM.Description = this.Description;

            controlORM.ControlListItemCollection = new TList<ControlListItem>();
            foreach (string s in this.List)
            {
                ControlListItem item = new ControlListItem();
                item.Status = 1;
                item.ItemData = s;
                controlORM.ControlListItemCollection.Add(item);
            }

            return controlORM;
        }
    }
}
